﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;


namespace PostscriptPreviewHandler
{
    /// <summary>
    /// Register the ps previewer using regasm.
    /// </summary>
    [RunInstaller(true)]
    public partial class InstallerRegisterPSAction : Installer
    {
        public InstallerRegisterPSAction()
        {
            InitializeComponent();
        }

        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Commit(IDictionary savedState)
        {
            base.Commit(savedState);

            RunRegAsm("/codebase");
        }

        /// <summary>
        /// Run regasm on our dll - with the given action (codebase or unregister!).
        /// </summary>
        /// <param name="action"></param>
        private static void RunRegAsm(string action)
        {
            string regAsmPath = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory() + "regasm.exe";
            string componentPath = typeof(InstallerRegisterPSAction).Assembly.Location;
            System.Diagnostics.Process.Start(regAsmPath, action + " \"" + componentPath + "\"");
        }

        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Install(IDictionary stateSaver)
        {
            base.Install(stateSaver);
        }

        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Uninstall(IDictionary savedState)
        {
            base.Uninstall(savedState);
            RunRegAsm("/unregister");
        }
    }
}
